﻿Imports System.Data.SqlClient
Imports CodeMatrix.Context
Imports System.Linq

Partial Class Apps_Shares_Uc_type_list
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                InitCtrl()
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Sub InitCtrl()
        'Hide column checkbox
        Dim btnDelete As Button = CType(gvDisplay.FindControl("btnDelete"), Button)
        If Not btnDelete Is Nothing Then
            btnDelete.Visible = Not String.IsNullOrEmpty(Request.QueryString("del"))
        End If
        ' Set breadcrump
        Dim BreadCrumpBG As HtmlControl = Page.Master.FindControl("tdBreadCrump")
        Dim BreadCrump As Literal = Page.Master.FindControl("ltrBreadCrump")
        If Not Request.QueryString("del") Is Nothing Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 20)
        ElseIf Not Request.QueryString("edit") Is Nothing Then
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 19)
        Else
            CodeMatrix.Business.GenericData.SetBreadCrump(BreadCrumpBG, BreadCrump, 17)
        End If
    End Sub

    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        Try
            'Filter data
            gvDisplay.DataBind()
            gvDisplay.Sort("type_name", SortDirection.Ascending)
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnDelete.Click
        Try
            Dim ctx As DataClassesDataContext = New DataClassesDataContext
            Dim keys As Int32() = CodeMatrix.Utilities.WebUiHelper.GetSelectedCheckBox(Of Int32)(gvDisplay, "type_code", "icbItem")
            Dim dataName As String = String.Empty
            For Each i As Int32 In keys
                Dim count As Int32 = ctx.stp_sub_menus.Count(Function(f) f.type_code = i)
                If count > 0 Then
                    dataName = ctx.master_types.SingleOrDefault(Function(f) f.type_code = i).type_name
                    Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, dataName))
                End If
            Next
            srcList.Delete()
            gvDisplay.DataBind()
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub


    Protected Sub gvDisplaySrc_Deleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles srcList.Deleting
        Try
            ' ============================================
            ' Assign parameter to SQL statement to delete
            ' ============================================
            Dim keys As Int32() = CodeMatrix.Utilities.WebUiHelper.GetSelectedCheckBox(Of Int32)(gvDisplay, "type_code", "icbItem")
            Dim ctx As DataClassesDataContext = New DataClassesDataContext

            For Each i In ctx.stp_sub_menus.Where(Function(f) keys.Contains(f.type_code))
                Dim type_name = ctx.master_types.SingleOrDefault(Function(f) f.type_code = i.type_code).type_name
                Throw New Exception(String.Format(Resources.Resource.MSG_CAUTION_DELETE_DEPEND_DATA, type_name))
            Next

            If keys.Length > 0 Then
                e.Command.Parameters("@cid").Value = CodeMatrix.Utilities.CodeHelper.CreateSeriesOfParameters(keys)
                Dim value As New OrderedDictionary()
                value.Add(e.Command.Parameters("@pid").Value, e.Command.Parameters("@cid").Value)
                GenericHelper.GenerateScript(GenerateScriptLogType.Delete, LogMenuType.master_type, value)
            Else
                e.Cancel = True
            End If
        Catch ex As Exception
            e.Cancel = True
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub srcList_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles srcList.Selecting
        Try
            If Not IsPostBack Then
                e.Cancel = True
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub srcList_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles srcList.Selected
        Try
            If e.AffectedRows > 0 Then
                ltrCountView.Text = String.Format("จำนวน {0} เร็คคอร์ด", e.AffectedRows)
            Else
                ltrCountView.Text = String.Empty
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try
    End Sub

    Protected Sub gvDisplay_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles gvDisplay.Sorting

        Try
            If (String.IsNullOrEmpty(e.SortExpression)) Then
                Dim oldSortCol As Image = gvDisplay.FindControl(gvDisplay.SortExpression)
                If Not oldSortCol Is Nothing Then oldSortCol.Visible = False
            Else
                Dim oldSortCol As Image = gvDisplay.FindControl(gvDisplay.SortExpression)
                Dim newSortCol As Image = gvDisplay.FindControl(e.SortExpression)

                If (Not oldSortCol Is Nothing) Then oldSortCol.Visible = False
                If (Not newSortCol Is Nothing) Then
                    newSortCol.Visible = True
                    If e.SortDirection = SortDirection.Ascending Then
                        newSortCol.ImageUrl = "../images/btn_down.png"
                    Else
                        newSortCol.ImageUrl = "../images/btn_up.png"
                    End If
                End If
            End If
        Catch ex As Exception
            GenericHelper.ShowAlert(Me, ex.Message)
        End Try

    End Sub
End Class
